//
//  DFParameter.m
//  dfutil
//
//  Created by Tom Booth on 25/06/2009.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "DFParameter.h"


@implementation DFParameter

@synthesize name;
@synthesize description;
@synthesize type;

@synthesize useInResponse;
@synthesize manditory;
@synthesize isSearchNumber;



- initWithXML:(NSXMLElement*) xml {
	self = [super init];
	
	// parse the xml out
	self.name = [[xml attributeForName:@"name"] stringValue];
	self.type = [DFParameter getTypeFromString:[[[xml nodesForXPath:[NSString stringWithFormat:@"%@%@",[xml XPath],@"//type"] error:NULL] objectAtIndex:0] stringValue]];
	
	if ([[xml nodesForXPath:[NSString stringWithFormat:@"%@%@",[xml XPath],@"//description"] error:NULL] count] > 0) {
		self.description = [[[xml nodesForXPath:[NSString stringWithFormat:@"%@%@",[xml XPath],@"//description"] error:NULL] objectAtIndex:0] stringValue];
	} else { self.description = @""; }
	
	if ([[xml nodesForXPath:[NSString stringWithFormat:@"%@%@",[xml XPath],@"//useinresponse"] error:NULL] count] > 0) {
		self.useInResponse = [DFUtility boolifyString:[[[xml nodesForXPath:[NSString stringWithFormat:@"%@%@",[xml XPath],@"//useinresponse"] error:NULL] objectAtIndex:0] stringValue]];
	} else { self.useInResponse = false; }
	
	if ([[xml nodesForXPath:[NSString stringWithFormat:@"%@%@",[xml XPath],@"//manditory"] error:NULL] count] > 0) {
		self.manditory = [DFUtility boolifyString:[[[xml nodesForXPath:[NSString stringWithFormat:@"%@%@",[xml XPath],@"//manditory"] error:NULL] objectAtIndex:0] stringValue]];
	} else { self.manditory = false; }
	
	if ([[xml nodesForXPath:[NSString stringWithFormat:@"%@%@",[xml XPath],@"//issearchnumber"] error:NULL] count] > 0) {
		self.isSearchNumber = [DFUtility boolifyString:[[[xml nodesForXPath:[NSString stringWithFormat:@"%@%@",[xml XPath],@"//issearchnumber"] error:NULL] objectAtIndex:0] stringValue]];
	} else { self.isSearchNumber = false; }
	
	return self;
}


- (NSUInteger) hashMe {
	NSUInteger r;
	
	switch (self.type) {
		case DFStringParameter:
			r=1;
			break;
		case DFNumberParameter:
			r=2;
			break;
		case DFBooleanParameter:
			r=3;
			break;
		case DFTimeParameter:
			r=4;
			break;
		case DFDateParameter:
			r=5;
			break;
		case DFAddressParameter:
			r=6;
			break;
		case DFLongParameter:
			r=7;
			break;
		default:
			r=0;
			break;
	}
	
	return r;
}



+ (DFParameterType) getTypeFromString:(NSString*) string {
	if ([string caseInsensitiveCompare:@"string"] == NSOrderedSame) { return DFStringParameter; }
	else if ([string caseInsensitiveCompare:@"number"] == NSOrderedSame) { return DFNumberParameter; }
	else if ([string caseInsensitiveCompare:@"boolean"] == NSOrderedSame) { return DFBooleanParameter; }
	else if ([string caseInsensitiveCompare:@"time"] == NSOrderedSame) { return DFTimeParameter; }
	else if ([string caseInsensitiveCompare:@"date"] == NSOrderedSame) { return DFDateParameter; }
	else if ([string caseInsensitiveCompare:@"address"] == NSOrderedSame) { return DFAddressParameter; }
	else if ([string caseInsensitiveCompare:@"long"] == NSOrderedSame) { return DFLongParameter; }
	else { return DFUnknownParameter; }
}

+ (NSString*) getStringFromType:(DFParameterType) t {
	if ( t == DFStringParameter ) { return @"String"; }
	else if ( t == DFNumberParameter ) { return @"Number"; }
	else if ( t == DFBooleanParameter ) { return @"Boolean"; }
	else if ( t == DFTimeParameter ) { return @"Time"; }
	else if ( t == DFDateParameter ) { return @"Date"; }
	else if ( t == DFAddressParameter ) { return @"Address"; }
	else if ( t == DFLongParameter ) { return @"Long"; }
	else if ( t == DFUnknownParameter ) { return @"Unknown"; }
	else { return @"blerg"; }
}

@end
